package com.template.service.impl;

import cn.hutool.core.util.IdUtil;
import com.template.common.BusinessException;
import com.template.common.PageData;
import com.template.common.ParameterException;
import com.template.dto.SysUserEditDto;
import com.template.dto.SysUserListDto;
import com.template.entity.SysUser;
import com.template.mapper.SysUserMapper;
import com.template.service.ISysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 系统用户表 服务实现类
 * </p>
 *
 * @author baomidou
 * @since 2021-12-22
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {


    @Override
    public PageData getPage(String keyword, Integer page, Integer pageSize) {
        if (page == null || pageSize == null) {
            throw new ParameterException("分页参数异常:page[" + page
                    + "],pageSize[" + pageSize + "]");
        }
        int count = baseMapper.getUserCount(keyword);
        if(count==0){
            return new PageData(0, Collections.EMPTY_LIST);
        }
        int start = PageData.calcFirstItemIndexOfPage(page, pageSize, count);
        List<SysUserListDto> list = baseMapper.getUserList(keyword,start,pageSize);
        return new PageData(count,list);
    }

    @Override
    public String createUser(SysUserEditDto req) {
        //校验可以用框架完成(比如:spring-boot-starter-validation,使用方式:https://blog.csdn.net/u013934408/article/details/108872775)
        if(StringUtils.isEmpty(req.getUsername())){
            throw new BusinessException("用户名不能为空");
        }
        if(StringUtils.isEmpty(req.getPassword())){
            throw new BusinessException("密码不能为空");
        }
        if(StringUtils.isEmpty(req.getRealname())){
            throw new BusinessException("昵称不能为空");
        }
        //todo 用户名全库重复校验
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(req,sysUser);//对象间值拷贝
        //补充字段
        sysUser.setId(IdUtil.getSnowflake().nextIdStr());//使用雪花算法
        //todo 密码加密
        sysUser.setDel(0);
        LocalDateTime now = LocalDateTime.now();
        sysUser.setCreateDataTime(now);
        sysUser.setModifyDataTime(now);
        if(baseMapper.insert(sysUser)>0){
            return sysUser.getId();
        }
        throw new BusinessException("新建失败");
    }

    @Override
    public Boolean updateUser(String id, SysUserEditDto req) {
        if(StringUtils.isEmpty(id)){
            throw new RuntimeException("id不能为空");
        }
        SysUser sysUser = baseMapper.selectById(id);
        if(sysUser==null){
            throw new BusinessException("用户不存在或已删除");
        }
        //修改数据
        if(!StringUtils.isEmpty(req.getUsername())){
            //todo 用户名全库重复校验
            sysUser.setUsername(req.getUsername());
        }
        if(!StringUtils.isEmpty(req.getPassword())){
            //todo 密码加密
            sysUser.setPassword(req.getPassword());
        }
        if(!StringUtils.isEmpty(req.getRealname())){
            sysUser.setRealname(req.getRealname());
        }
        sysUser.setModifyDataTime(LocalDateTime.now());
        return baseMapper.updateById(sysUser)>0;
    }

    @Override
    public Integer deleteUser(String id) {
        if(StringUtils.isEmpty(id)){
            throw new ParameterException("id不能为空");
        }
        return baseMapper.deleteUser(id,LocalDateTime.now());
    }
}
